Tengo una matriz inicial, array1 que contiene 30 elementos y necesito crear una nueva matriz array2 que contenga los elementos 0-14 de array1 .
Estoy usando IntStream como mapeador para el índice de matriz. Pero lo siguiente da errores:
Object[] array2 = IntStream.range(0,14).map(x -> (Object)array1[x]).toArray(Object[]::new); Error en (Object)array1[x] :
The type of the expression must be an array type but it resolved to List<Object[]>En su ejemplo, x es un tipo de número entero. Debe usar .mapToObj en lugar del método map .
Object[] array2 = IntStream.range(0, 14) .mapToObj(x -> array1[x]) .toArray(Object[]::new);También puede usar Arrays.copyOfRange() en lugar de Stream .
Object[] array2 = Arrays.copyOfRange(array1, 0, 14);Para obtener los primeros 15 (0-14) elementos de una matriz a una matriz
List<String> first15ElementsList = Arrays.stream(arr) .limit(15) .collect(Collectors.toList());